package com.security.config.filter;

import com.alibaba.fastjson.JSON;
import com.security.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Component("securitySuccessHandler")
public class SecuritySuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess
            (HttpServletRequest req, HttpServletResponse res, Authentication auth)
            throws IOException, ServletException {
       log.info("-----------------认证成功-----------------");
       res.setContentType("application/json;charset=UTF-8");
       res.getWriter().write(JSON.toJSONString(R.success("登录成功","/security/index")));
    }
}
